package com.newlink.building.userinfo.activity

import android.content.Intent
import android.provider.MediaStore
import android.view.View
import androidx.core.content.FileProvider
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker
import java.io.File

/**
 * @Author: Jake
 * @Date: 2023/1/6
 * @Description:
 */
class Module_UserInfo_NotificationActivityNL : NL_BaseActivity() {

    companion object {
        var APP_PATH: String = "FVL_APP_PATH"
        var APP_NAME: String = "FVL_APP_NAME"
    }

    private var apkFile: File? = null
    private var appPath: String? = null
    private var appName: String? = null
//    override fun attachLayoutRes(): Int = 0

    override fun attachLayoutRes(): View? = null

    override fun initData() {
        // 通知处理页面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("NotificationActivity", "initData")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom("notification")

        appPath = intent.getStringExtra(APP_PATH)
        appName = intent.getStringExtra(APP_NAME)

        installDownloadApk()
        finish()
    }

    override fun initView() {

    }


    private fun installDownloadApk() {
        // APK安装时的混淆
        Module_Obfuscation_ObfuscationInvoker.invokeSpecific(12, appName)

        apkFile = File(appPath, appName)
        apkFile?.let {
            if (!it.exists()) {
                return
            }
            val intent = Intent()
            intent.action = Intent.ACTION_VIEW

            val publicUri = FileProvider.getUriForFile(
                this,
//                getString(R.string.file_provider),
                "$packageName.fileprovider",
                apkFile!!
            )

            intent.putExtra(MediaStore.EXTRA_OUTPUT, publicUri)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.setDataAndType(publicUri, "application/vnd.android.package-archive")

            startActivity(intent)
        }
    }
}
